<?php

	class EventItemDataAccessMySQL implements iEventItemDataAccess {
		
		public function buildObject($result) {
			
			$eventItem = new EventItem();
			
			$eventItem->setEventItemId($result->eventItemId);
			$eventItem->setEventId($result->eventId);		
			$eventItem->setDescription($result->description);	
			$eventItem->setName($result->name);
			$eventItem->setStartDate(formatDate($result->startDate));
			$eventItem->setEndDate(formatDate($result->endDate));
			$eventItem->setRegionId($result->regionId);
			
			$eventItemUser = new EventItemUser();
			
			$eventUser = new EventUser();
			$eventUser->setUserRegistrationCode($result->registrationCode);
			
			$eventItemUser->setIsUserSpeaker($result->isSpeaker);
			$eventItemUser->setEventUser($eventUser);
			
			$eventItem->setEventItemUser($eventItemUser);
			
			return $eventItem;
		}	
		
		public function selectEventItemByEventId($eventId, $dataBaseConnection) {
						
			$resultArray = array();
			
			$parameters["EVENTID"]    = $eventId;
			
			$query = buildQuery("querySelectEventItemByEventId", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function select($eventItem, $dataBaseConnection) {
						
			$resultArray = array();
			
			if ($eventItem->getEventItemUser()->getEventUser()->getUser()->getUserId() != null){
				$parameters["USERID"] = $eventItem->getEventItemUser()->getEventUser()->getUser()->getUserId();	
			} else {
				$parameters["USERID"] = "NULL";
			}
			
			if ($eventItem->getEventItemId() != null){
				$parameters["EVENTITEMID"] = $eventItem->getEventItemId();
			}else {
				$parameters["EVENTITEMID"] = "NULL";
			}
			
			if ($eventItem->getEventId() != null){
				$parameters["EVENTID"] = $eventItem->getEventId();
			}else {
				$parameters["EVENTID"] = "NULL";
			}
			if ($eventItem->getName() != null){
				$parameters["NAME"] = "'" . $eventItem->getName() . "'";
			}else {
				$parameters["NAME"] = "NULL";
			}
			if ($eventItem->getDescription() != null){
				$parameters["DESCRIPTION"] = "'" . $eventItem->getDescription() . "'";
			}else {
				$parameters["DESCRIPTION"] = "NULL";
			}			
			if ($eventItem->getStartDate() != null){
				$parameters["STARTDATE"] = "'" . formatDateMySQL($eventItem->getStartDate()) . "'";
			}else {
				$parameters["STARTDATE"] = "NULL";
			}
			if ($eventItem->getEndDate() != null){
				$parameters["ENDDATE"] = "'" . formatDateMySQL($eventItem->getEndDate()) . "'";
			}else {
				$parameters["ENDDATE"] = "NULL";
			}
			if ($eventItem->getRegionId() != null){
				$parameters["REGIONID"] = "'" . $eventItem->getRegionId() . "'";
			}else {
				$parameters["REGIONID"] = "NULL";
			}
			
			$query = buildQuery("querySelectEventItem", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}

		public function saveEventItemAttendant($eventItem, $dataBaseConnection) {
						
			$resultArray = array();
			
			$parameters["EVENTITEMID"]    = $eventItem->getEventItemId();
			$parameters["REGISTRATIONCODE"]    = $eventItem->getEventItemUser()->getEventUser()->getUserRegistrationCode();
			
			if ($eventItem->getEventItemUser()->getIsUserSpeaker()){
				$parameters["ISSPEAKER"] = "true";
			}else {
				$parameters["ISSPEAKER"] = "false";
			}
			
			$query = buildQuery("queryInsertEventItemAttendant", $parameters);
			
			
			if (!is_bool($query)) {
				$result = $dataBaseConnection->query($query);
						
				return $result;
			} else {
				return false;
			}
		}
		
		// public function deleteEventItemAttendant($eventItem, $dataBaseConnection) {
// 			
			// $resultArray = array();
// 			
			// if ($eventItem->getEventItemId() != null){
				// $parameters["EVENTITEMID"] = $eventItem->getEventItemId();
			// }else {
				// $parameters["EVENTITEMID"] = "NULL";
			// }
// 			
			// if ($eventItem->getEventItemUser()->getEventUser()->getUserRegistrationCode() != null){
				// $parameters["REGISTRATIONCODE"] = $eventItem->getEventItemUser()->getEventUser()->getUserRegistrationCode();
			// }else {
				// $parameters["REGISTRATIONCODE"] = "NULL";
			// }
// 					
			// $query = buildQuery("queryDeleteEventItemAttendant", $parameters);
			// if (!is_bool($query)) {
				// $result = $dataBaseConnection->query($query);
// 				
				// return $result;
			// } else {
				// return false;
			// }
		// }
	}

?>